#include <glib.h>

static void _ptr_array_foreach_print_func(gpointer data, gpointer user_data)
{
    g_print("%s: %s(addr:%p) \n", (gchar *)user_data, (gchar *)data, data);
}

static void _ptr_array_free_func(gpointer data)
{
    if(NULL != data) {
        g_free(data);
    }
}

gint main(gint argc, gchar **argv)
{
    GPtrArray *parr = NULL;
    gboolean ret = FALSE;
    gpointer p = NULL;
    gchar str0[8] = {0};
    gchar str1[8] = {0};
    gchar str2[8] = {0};
    gchar str3[8] = {0};
    gchar str4[8] = {0};
    gchar str5[8] = {0};
    gchar str6[8] = {0};
    
    g_strlcpy(str0, "one", 8);
    g_strlcpy(str1, "two", 8);
    g_strlcpy(str2, "two", 8);
    g_strlcpy(str3, "three", 8);
    g_strlcpy(str4, "four", 8);
    g_strlcpy(str5, "five", 8);
    g_strlcpy(str6, "six", 8);

    parr = g_ptr_array_sized_new(10);
    
    g_ptr_array_add(parr, (gpointer)g_strdup(str0));
    g_ptr_array_add(parr, (gpointer)g_strdup(str1));
    g_ptr_array_add(parr, (gpointer)g_strdup(str2));
    g_ptr_array_add(parr, (gpointer)g_strdup(str3));
    g_ptr_array_add(parr, (gpointer)g_strdup(str4));
    g_ptr_array_add(parr, (gpointer)g_strdup(str5));
    g_ptr_array_add(parr, (gpointer)g_strdup(str6));

    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "ori");

    g_ptr_array_set_free_func(parr, _ptr_array_free_func);

    ret = g_ptr_array_remove(parr, "two");
    g_print("remove(two) ret: %s \n", ret ? "TRUE" : "FALSE");
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove(two)");

    p = g_ptr_array_remove_index(parr, 1);
    g_print("remove_index(1) p:%p \n", p);
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove_index(1)");

    ret = g_ptr_array_remove_fast(parr, "three");
    g_print("remove_fast(three) ret: %s \n", ret ? "TRUE" : "FALSE");
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove_fast(three)");

    p = g_ptr_array_remove_index_fast(parr, 2);
    g_print("remove_index_fast(2) p:%p \n", p);
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove_index_fast(2)");

    g_ptr_array_remove_range(parr, 0, 2);
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove_range(0,2)");

    g_ptr_array_free(parr, TRUE);

    return 0;
}